//
//  MainNewsCollectionController.swift
//  lookLikeIweekly
//
//  Created by Mainzxq on 16/6/12.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class MainNewsCollectionController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    var refresher: UIRefreshControl = UIRefreshControl()
    var myScroll: NewsScrollView = NewsScrollView()
    var newsArray = [PictureArticleSection]()
    var aiv = UIActivityIndicatorView()
    override init(collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(collectionViewLayout: layout)
        //self.newsArray = workNewsOut(titleArray[0].1)

        self.collectionView = NewsCollectionView(frame: CGRectMake(0, 0, WIDTH, HEIGHT-365), collectionViewLayout: OneColumnLayOut(count: newsArray.count))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        refresher.backgroundColor = UIColor.clearColor()
        refresher.tintColor = UIColor.clearColor()
        refresher.attributedTitle = NSAttributedString(string: "下拉刷新")
        refresher.addTarget(self, action: #selector(self.loadData), forControlEvents: .ValueChanged)
        aiv.frame.origin = refresher.frame.origin
        aiv.frame.size = CGSizeMake(50, 50)
        aiv.activityIndicatorViewStyle = .Gray
        self.collectionView?.addSubview(aiv)
        self.collectionView?.addSubview(refresher)
        setupCollectionView()
        

    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return newsArray.count
    }

    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainNewsCell
        if indexPath.row < 1 {
            cell.imageView.image = self.newsArray[indexPath.row].getImage()
        } else {
            cell.imageView.image = self.newsArray[indexPath.row].albumImage
        }
        cell.contentLbl.text = self.newsArray[indexPath.row].writer
        cell.titleLbl.text = self.newsArray[indexPath.row].article
        return cell
    }
    
    
    override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        let cell = cell as! MainNewsCell
//        cell.imageView.image = self.newsArray[indexPath.row].albumImage

    }
    
    
    func setupCollectionView() {
        let myLayOut = OneColumnLayOut(count: newsArray.count)
        self.collectionView?.showsVerticalScrollIndicator=false
        self.collectionView?.showsHorizontalScrollIndicator=false
        self.collectionView?.collectionViewLayout = myLayOut
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        self.collectionView?.frame = CGRectMake(0, 0, WIDTH, HEIGHT-285)
        self.collectionView?.dataSource = self
        self.collectionView?.delegate = self
        self.collectionView!.registerClass(MainNewsCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    func loadData() {
        self.refresher.beginRefreshing()
        print("loadData by refresher")

        aiv.startAnimating()
        
        self.refresher.endRefreshing()
        aiv.stopAnimating()
    }
    

}
